﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Apex.Common.Base.ViewModels;
using Apex.Script.Models;
using Apex.Common.MVVM;
using System.ComponentModel;

namespace Apex.Script.ViewModels
{
    public interface ISceneObjectViewModel : ISceneObject, IViewModel
    {
    }

    public class DialogueItemViewModel : ViewModel<DialogueItem>
    {
        public Character Character
        {
            get { return Model.Character; }
            set { Model.Character = value; OnPropertyChanged("Character"); }
        }

        public string Line
        {
            get { return Model.Line; }
            set { Model.Line = value; OnPropertyChanged("Line"); }
        }

        public string Directions
        {
            get { return Model.Directions; }
            set { Model.Directions = value; OnPropertyChanged("Directions"); }
        }

        public string TreeDisplayText
        {
            get { return string.Format("{0} : {1}", Character.Name, Line); }
        }

        public DialogueItemViewModel()
            : base()
        {

        }

        public DialogueItemViewModel(DialogueItem model)
            : base(model)
        {

        }
    }

    public class SceneDialogueViewModel : CollectionViewModel<SceneDialogue, DialogueItemViewModel, DialogueItem>, ISceneObjectViewModel
    {
        public string Type
        {
            get { return Model.Type; }
            set { Model.Type = value; OnPropertyChanged("Type"); }
        }

        public string TreeDisplayText
        {
            get { return "Dialogue"; }
        }

        public SceneDialogueViewModel()
            : base()
        {

        }

        public SceneDialogueViewModel(SceneDialogue model)
            : base(model)
        {
            _items.CollectionChanged -= _items_CollectionChanged;
            foreach (var x in model.Items)
            {
                _items.Add(new DialogueItemViewModel(x));
            }
            _items.CollectionChanged += _items_CollectionChanged;
        }
    }

    public class SceneDescriptionViewModel : ViewModel<SceneDescription>, ISceneObjectViewModel
    {
        public string Type
        {
            get { return Model.Type; }
            set { Model.Type = value; OnPropertyChanged("Type"); }
        }

        public string Description
        {
            get { return Model.Description; }
            set { Model.Description = value; OnPropertyChanged("Description"); }
        }

        public string TreeDisplayText
        {
            get { return "Description: " + Description; }
        }

        public SceneDescriptionViewModel()
            : base()
        {
        }

        public SceneDescriptionViewModel(SceneDescription model)
            : base(model)
        {
        }
    }

    public class SceneViewModel : CollectionViewModel<Scene, ISceneObjectViewModel, ISceneObject>
    {
        private LocationViewModel _location;
        private bool _isCharactersChecked;
        private bool _isLocationChecked;

        public int ID
        {
            get { return Model.ID; }
            set { Model.ID = value; OnPropertyChanged("ID"); }
        }

        public string Name
        {
            get { return Model.Name; }
            set { Model.Name = value; OnPropertyChanged("Name"); }
        }

        public string Description
        {
            get { return Model.Description; }
            set { Model.Description = value; OnPropertyChanged("Description"); }
        }

        public LocationViewModel Location
        {
            get { return _location; }
            set { _location = value; OnPropertyChanged("Location"); }
        }

        public string TreeDisplayText
        {
            get { return Name; }
        }

        public List<Character> Characters { get { return Model.Characters; } }


        public bool IsCharactersChecked
        {
            get { return _isCharactersChecked; }
            set
            {
                _isCharactersChecked = value;
                _isLocationChecked = false;
                OnPropertyChanged("IsCharactersChecked");
                OnPropertyChanged("IsLocationChecked");
            }
        }

        public bool IsLocationChecked
        {
            get { return _isLocationChecked; }
            set
            {
                _isLocationChecked = value;
                _isCharactersChecked = false;
                OnPropertyChanged("IsLocationChecked");
                OnPropertyChanged("IsCharactersChecked");
            }
        }

        public string Group { get; set; }
        public SceneViewModel()
            : base()
        {
            _location = new LocationViewModel();
            Group = "Scene Descriptions";
        }

        public SceneViewModel(Scene model)
            : base(model)
        {
            _location = new LocationViewModel(model.Location);
            Group = "Scene Descriptions";
            _items.CollectionChanged -= _items_CollectionChanged;
            foreach (var x in model.Items)
            {
                if (x is SceneDialogue)
                    _items.Add(new SceneDialogueViewModel((SceneDialogue)x));

                if (x is SceneDescription)
                    _items.Add(new SceneDescriptionViewModel((SceneDescription)x));
            }
            _items.CollectionChanged += _items_CollectionChanged;
        }
    }
}
